package com.zdkj.common.security.util;

import com.zdkj.common.core.constant.CommonConstants;
import com.zdkj.common.security.service.PigxUser;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @support: 三永华通科技有限公司
 * @author: cui_com
 * @version: 1.0
 * @Date: 2020-01-13 15:17:37.
 * @Description: <p>
 * 对象工具类
 * </p>
 */
@Slf4j
@UtilityClass
public class ObjectUtils {
    public <T> T getDefaultObj(Class<T> cls) {
        T obj = null;
        try {
            obj = cls.newInstance();
            setDefaultFiledValue(obj);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public void setDefaultFiledValue(Object obj) {
        Class clz = obj.getClass();
        PigxUser user = SecurityUtils.getUser();
        Map<String, Object> filedMap = new HashMap<String, Object>(7) {{
            if (null != user) {
                put("creatorId", user.getId());
                put("creatorName", user.getNikeName());
                put("creatorCode", user.getUserCode());
                put("deptId", user.getDeptId());
                put("lineCode", user.getLineCode());
                put("stationCode", user.getStationCode());
            }else {
                put("creatorId", 1);
                put("creatorName", "管理员");
                put("creatorCode", "001");
                put("deptId", 1);
            }
            put("createTime", LocalDateTime.now());
            put("delFlag", CommonConstants.STATUS_NORMAL);
        }};

        for (String filedName : filedMap.keySet()) {
            Field field;
            try {
                field = clz.getDeclaredField(filedName);
                field.setAccessible(true);
                field.set(obj, filedMap.get(filedName));
            } catch (NoSuchFieldException e) {
                try {
                    field = clz.getSuperclass().getDeclaredField(filedName);
                    field.setAccessible(true);
                    field.set(obj, filedMap.get(filedName));
                } catch (NoSuchFieldException | IllegalAccessException e1) {
                    //log.warn(e1.toString());
                }
                //log.warn(e.toString());
            } catch (IllegalAccessException e) {
                log.error(e.toString());
            } finally {
                continue;
            }
        }
    }

    public void setDefaultFiledValueOfBelongToLine(Object obj) {
        Class clz = obj.getClass();
        PigxUser user = SecurityUtils.getUser();
        Map<String, Object> filedMap = new HashMap<String, Object>(7) {{
            if (null != user) {
                put("creatorId", user.getId());
                put("creatorName", user.getNikeName());
                put("creatorCode", user.getUserCode());
                put("deptId", user.getDeptId());
            }
            put("createTime", LocalDateTime.now());
            put("delFlag", CommonConstants.STATUS_NORMAL);
        }};

        for (String filedName : filedMap.keySet()) {
            Field field;
            try {
                field = clz.getDeclaredField(filedName);
                field.setAccessible(true);
                field.set(obj, filedMap.get(filedName));
            } catch (NoSuchFieldException e) {
                try {
                    field = clz.getSuperclass().getDeclaredField(filedName);
                    field.setAccessible(true);
                    field.set(obj, filedMap.get(filedName));
                } catch (NoSuchFieldException | IllegalAccessException e1) {
                    //log.warn(e1.toString());
                }
                //log.warn(e.toString());
            } catch (IllegalAccessException e) {
                log.error(e.toString());
            } finally {
                continue;
            }
        }
    }

    public String[] getNullAndEmptyPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for (PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null || "".equals(srcValue)) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
}
